package com.artPreter.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.artPreter.pojo.Depart;
import com.artPreter.pojo.Result;
import com.artPreter.pojo.User;
import com.artPreter.service.IDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 艺术机构表 前端控制器
 * </p>
 *
 * @author jarvis
 * @since 2024-11-14
 */
@RestController
@RequestMapping("/depart")
public class DepartController {

    @Autowired
    private IDepartService departService;

    /**
     * 机构分页
     * @param currentPage
     * @param size
     * @param
     * @return
     */
    @PostMapping("/getPage")
    public Result getPage(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage, @RequestParam(value = "size",defaultValue = "10") Integer size, @RequestBody Depart depart){
        IPage<Depart> departIPage = departService.getPage(currentPage,size,depart);
        return Result.success(departIPage);
    }

    /**
     * 添加机构
     * @param depart
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Depart depart){
        departService.add(depart);
        return Result.success();
    }

    /**
     * 修改艺术机构
     * @param depart
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Depart depart){
        departService.edit(depart);
        return Result.success();
    }

    /**
     * 查询艺术机构详情
     * @param depart
     * @return
     */
    @PostMapping("/getInfo")
    public Result getInfo(@RequestBody Depart depart){
        Depart result = departService.getInfo(depart);
        return Result.success(result);
    }

    /**
     * 软删除艺术机构
     * @param depart
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Depart depart){
        departService.delete(depart);
        return Result.success();
    }

    /**
     * 保存排序
     * @param departList
     * @return
     */
    @PostMapping("/saveDepartSort")
    public Result edit(@RequestBody List<Depart> departList){
        for (int i = 0; i < departList.size(); i++) {
            Depart depart = new Depart();
            if(departList.get(i).getId()!=null&&departList.get(i).getSort()!=null){
                depart.setId(departList.get(i).getId());
                depart.setSort(departList.get(i).getSort());
                departService.edit(depart);
            }
        }
        return Result.success();
    }

}
